package org.jix.gbook.note.biz.rpc;

import jakarta.annotation.Resource;
import org.jix.framework.common.response.Response;
import org.jix.gbook.user.api.UserFeignApi;
import org.jix.gbook.user.dto.req.FindUserByIdReqDTO;
import org.jix.gbook.user.dto.resp.FindUserByIdRspDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author stark
 * @date 2025/9/22 上午10:53
 * @description
 **/
@Component
public class UserRpcService {

    private static final Logger log = LoggerFactory.getLogger(UserRpcService.class);
    @Resource
    private UserFeignApi userFeignApi;

    public UserRpcService(UserFeignApi userFeignApi) {
        this.userFeignApi = userFeignApi;
    }

    /**
     * 查询用户信息
     * @param userId
     * @return
     */
    public FindUserByIdRspDTO findById(Long userId) {
        FindUserByIdReqDTO findUserByIdReqDTO = new FindUserByIdReqDTO();
        findUserByIdReqDTO.setId(userId);

        Response<FindUserByIdRspDTO> response = userFeignApi.findById(findUserByIdReqDTO);

        //获取id失败
        if (Objects.isNull(response) || !response.isSuccess()) {
            log.info("can not find user by id: {}", userId);
            return null;
        }

        return response.getData();
    }
}
